package com.saku.uiprojecttest.activities;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.saku.uiprojecttest.R;
import com.saku.uiprojecttest.SakuApplication;
import com.saku.uiprojecttest.bean.PagerItem;
import com.saku.uiprojecttest.schema.LMRouterManager;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {
    public static final String TAG = MainActivity.class.getSimpleName();

    private int[] mResIds;
    private int mSpacing;
    private ViewPager mViewPager;
    private List<PagerItem> mPagerList;
    private LinearLayout mLlDots;
    private int mLastPos;
    private RecyclerView recyclerView;

    @Inject
    public Gson mGson;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setRecyclerView();

        setViewPager();

        initDots();

        SakuApplication.getComponent().inject(this);
        final LinearLayout animLl = (LinearLayout) findViewById(R.id.anim_ll);
        final TextView animTv = (TextView) findViewById(R.id.anim_tv);
        final ImageView animIv = (ImageView) findViewById(R.id.anim_iv);

        final Button btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ObjectAnimator ob = ObjectAnimator.ofFloat(btn, "translationY", 0, -btn.getHeight());
                ob.setDuration(500);
                ob.start();

                LMRouterManager.init(getApplicationContext());
//                LMRouterManager.getRouter().goToTestActivity("1", "the first one");
                LMRouterManager.getRouter().go2TestActivityAnotherData("1", "the first one");

//                Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("lm://goods:4321/goodsDetail"));
//                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                startActivity(i);
            }
        });

        final ViewGroup.MarginLayoutParams rvMarginLp = (ViewGroup.MarginLayoutParams) recyclerView.getLayoutParams();
        final ViewGroup.LayoutParams lp = animLl.getLayoutParams();
        Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(Subscriber<? super String> subscriber) {
                subscriber.onCompleted();
            }
        }).delay(1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<String>() {
                    @Override
                    public void onCompleted() {
                        Log.d("123", "onCompleted");
                        ValueAnimator openLlAnim = ValueAnimator.ofInt(0, animLl.getHeight());
                        openLlAnim.setDuration(500);
                        openLlAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                            @Override
                            public void onAnimationUpdate(ValueAnimator animation) {
//                                Log.d("123", "onAnimationUpdate : " + animation.getAnimatedValue());
                                rvMarginLp.topMargin = (20 + (int)animation.getAnimatedValue());
                                recyclerView.setLayoutParams(rvMarginLp);
                                lp.height = (int) animation.getAnimatedValue();
                            }
                        });

                        ObjectAnimator openLimitTvAnim = ObjectAnimator.ofFloat(animTv, "alpha", 0, 1);
                        ObjectAnimator openLimitIvAnim = ObjectAnimator.ofFloat(animIv, "alpha", 0, 1);

                        AnimatorSet twoAnim = new AnimatorSet();
                        twoAnim.playTogether(openLimitIvAnim, openLimitTvAnim);
                        twoAnim.setDuration(1000);
                        twoAnim.addListener(new AnimatorListenerAdapter() {
                            @Override
                            public void onAnimationStart(Animator animation) {
                                animTv.setVisibility(View.VISIBLE);
                                animIv.setVisibility(View.VISIBLE);
                            }
                        });
                        AnimatorSet animatorAllSet = new AnimatorSet();
                        animatorAllSet.playSequentially(openLlAnim, twoAnim);
                        animatorAllSet.start();
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(String s) {

                    }
                });
    }

    private void initDots() {
        RelativeLayout pagerRl = (RelativeLayout) findViewById(R.id.pagerRl);
        mLlDots = (LinearLayout) findViewById(R.id.ll_dots);
        int pagerCount = (int) ((mPagerList.size() / 2.0f) + 0.5);

        int dotLayoutHeight = getResources().getDimensionPixelSize(R.dimen.dot_layout_height);
        int dotMargin = getResources().getDimensionPixelSize(R.dimen.dot_margin);
        int dotWidth = getResources().getDimensionPixelSize(R.dimen.dot_width);

        for (int i = 0; i < pagerCount; i++) {
            ImageView dotView = new ImageView(this);
            if (i == 0) {
                dotView.setBackgroundResource(R.drawable.dot_circle_selected);
            } else {
                dotView.setBackgroundResource(R.drawable.dot_circle);
            }
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(dotWidth, dotWidth);
            if (i != 0) {
                lp.setMargins(dotMargin, 0, 0, 0);
            }
            dotView.setLayoutParams(lp);

            mLlDots.addView(dotView);
        }
    }

    private void setViewPager() {
        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        final CarPagerAdapter carPagerAdapter = new CarPagerAdapter(this, mPagerList);
        mViewPager.setAdapter(carPagerAdapter);
        mViewPager.setPageMargin(mSpacing);
        mViewPager.setOffscreenPageLimit(4); //设置缓存页面
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                Log.d("main-activity", "onPageScrolled -- position: " + position + ", positionOffset: "
                        + positionOffset + ", positionOffsetPixels: " + positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                Log.d("main-activity", "onPageSelected -- position: " + position);
                ((ImageView) mLlDots.getChildAt(mLastPos)).setImageResource(R.drawable.dot_circle);
                ((ImageView) mLlDots.getChildAt(position)).setImageResource(R.drawable.dot_circle_selected);

                mLastPos = position;

            }

            @Override
            public void onPageScrollStateChanged(int state) {
                Log.d("main-activity", "onPageScrollStateChanged -- state: " + state);
            }
        });

        mViewPager.setVisibility(View.GONE);
    }

    private void setRecyclerView() {
        mPagerList = new ArrayList<>();
        mPagerList.add(new PagerItem(R.drawable.betty, "betty"));
        mPagerList.add(new PagerItem(R.drawable.cake, "cake"));
        mPagerList.add(new PagerItem(R.drawable.flowers, "flowers"));
        mPagerList.add(new PagerItem(R.drawable.mad, "mad"));
        mPagerList.add(new PagerItem(R.drawable.qianniuhua, "qianniuhua"));
        recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        recyclerView.setAdapter(new CarViewAdapter(this, mPagerList));
        mSpacing = this.getResources().getDimensionPixelOffset(R.dimen.recycler_view_spacing);
        recyclerView.addItemDecoration(new CarViewAdapter.CarItemDecoration(mSpacing));

//        recyclerView.setVisibility(View.GONE);
    }

}
